<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="状态;1:已发布;0:未发布;" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['postStatus', validatorRules.postStatus]" :trigger-change="true" dictCode="post_status" placeholder="请选择状态;1:已发布;0:未发布;"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="评论状态;1:允许;0:不允许" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['commentStatus', validatorRules.commentStatus]" :trigger-change="true" dictCode="comment_status" placeholder="请选择评论状态;1:允许;0:不允许"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否置顶;1:置顶;0:不置顶" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isTop', validatorRules.isTop]" :trigger-change="true" dictCode="is_top" placeholder="请选择是否置顶;1:置顶;0:不置顶"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否推荐;1:推荐;0:不推荐" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['recommended', validatorRules.recommended]" :trigger-change="true" dictCode="recommended" placeholder="请选择是否推荐;1:推荐;0:不推荐"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="查看数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['postHits', validatorRules.postHits]" placeholder="请输入查看数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="收藏数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['postFavorites', validatorRules.postFavorites]" placeholder="请输入收藏数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="点赞数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['postLike', validatorRules.postLike]" placeholder="请输入点赞数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="评论数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['commentCount', validatorRules.commentCount]" placeholder="请输入评论数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['createTime', validatorRules.createTime]" placeholder="请输入创建时间" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['updateTime', validatorRules.updateTime]" placeholder="请输入更新时间" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发布时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['publishedTime', validatorRules.publishedTime]" placeholder="请输入发布时间" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="删除时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['deleteTime', validatorRules.deleteTime]" placeholder="请输入删除时间" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="post标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['postTitle', validatorRules.postTitle]" placeholder="请输入post标题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="seo keywords" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['postKeywords', validatorRules.postKeywords]" placeholder="请输入seo keywords"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="post摘要" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['postExcerpt', validatorRules.postExcerpt]" placeholder="请输入post摘要"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="转载文章的来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['postSource', validatorRules.postSource]" placeholder="请输入转载文章的来源"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="缩略图" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['thumbnail', validatorRules.thumbnail]"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="文章内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-editor v-decorator="['postContent',{trigger:'input'}]"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    name: 'LbPortalPostForm',
    components: {
      JFormContainer,
      JImageUpload,
      JDictSelectTag,
      JEditor,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          postStatus: {
            rules: [
              { required: true, message: '请输入状态;1:已发布;0:未发布;!'},
            ]
          },
          commentStatus: {
            rules: [
              { required: true, message: '请输入评论状态;1:允许;0:不允许!'},
            ]
          },
          isTop: {
            rules: [
              { required: true, message: '请输入是否置顶;1:置顶;0:不置顶!'},
            ]
          },
          recommended: {
            rules: [
              { required: true, message: '请输入是否推荐;1:推荐;0:不推荐!'},
            ]
          },
          postHits: {
            rules: [
              { required: true, message: '请输入查看数!'},
            ]
          },
          postFavorites: {
            rules: [
              { required: true, message: '请输入收藏数!'},
            ]
          },
          postLike: {
            rules: [
              { required: true, message: '请输入点赞数!'},
            ]
          },
          commentCount: {
            rules: [
              { required: true, message: '请输入评论数!'},
            ]
          },
          createTime: {
            rules: [
              { required: true, message: '请输入创建时间!'},
            ]
          },
          updateTime: {
            rules: [
              { required: true, message: '请输入更新时间!'},
            ]
          },
          publishedTime: {
            rules: [
              { required: true, message: '请输入发布时间!'},
            ]
          },
          deleteTime: {
            rules: [
              { required: true, message: '请输入删除时间!'},
            ]
          },
          postTitle: {
            rules: [
              { required: true, message: '请输入post标题!'},
            ]
          },
          postKeywords: {
            rules: [
              { required: true, message: '请输入seo keywords!'},
            ]
          },
          postExcerpt: {
            rules: [
              { required: true, message: '请输入post摘要!'},
            ]
          },
          postSource: {
            rules: [
              { required: true, message: '请输入转载文章的来源!'},
            ]
          },
          thumbnail: {
            rules: [
              { required: true, message: '请输入缩略图!'},
            ]
          },
        },
        url: {
          add: "/bobo.cms/lbPortalPost/add",
          edit: "/bobo.cms/lbPortalPost/edit",
          queryById: "/bobo.cms/lbPortalPost/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'postStatus','commentStatus','isTop','recommended','postHits','postFavorites','postLike','commentCount','createTime','updateTime','publishedTime','deleteTime','postTitle','postKeywords','postExcerpt','postSource','thumbnail','postContent'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'postStatus','commentStatus','isTop','recommended','postHits','postFavorites','postLike','commentCount','createTime','updateTime','publishedTime','deleteTime','postTitle','postKeywords','postExcerpt','postSource','thumbnail','postContent'))
      },
    }
  }
</script>